package utilJsonRpc

import "github.com/smallnest/rpcx/server"

type JsonRpcServer struct {
	addr   string
	Server *server.Server
}

func NewServer(addr string) *JsonRpcServer {

	s := server.NewServer()
	return &JsonRpcServer{Server: s, addr: addr}
}

func (js *JsonRpcServer) SetCors(allowedOrigins []string, allowedMethods []string, allowedHeaders []string, allowCredentials bool) {
	opt := server.CORSOptions{
		AllowedOrigins: allowedOrigins,
		//AllowOriginFunc:        nil,
		//AllowOriginRequestFunc: nil,
		AllowedMethods: allowedMethods,
		AllowedHeaders: allowedHeaders,
		//ExposedHeaders:         nil,
		//MaxAge:                 0,
		AllowCredentials: allowCredentials,
		//OptionsPassthrough:     false,
		//OptionsSuccessStatus:   0,
		//Debug:                  false,
	}
	js.SetCorsOptions(&opt)
}

func (js *JsonRpcServer) SetCorsOptions(corsOpt *server.CORSOptions) {
	js.Server.SetCORS(corsOpt)
}
func (js *JsonRpcServer) SetCorsAllowAll() {
	js.Server.SetCORS(server.AllowAllCORSOptions())
}
func (js *JsonRpcServer) RegisterReceiver(name string, rcvr interface{}, metadata string) {
	js.Server.RegisterName(name, rcvr, metadata)
}

func (js *JsonRpcServer) Run() error {
	return js.Server.Serve("tcp", js.addr)
}
